/*
@hehung
2023-2-8
转载请注明出处，版权由@hehung所有
email: 1398660197@qq.com
wechat: hehung95
*/

#include "app_wifi.h"
#include "da16200_AT.h"
#include "hal_data.h"
#include "app_led.h"
#include "app_common.h"
#include "app_rtc.h"
#include "app_isl29035.h"
#include "app_hs300x.h"
#include "app_mq5.h"
#include <stdio.h>
#include <string.h>

#define WIFI_DEBUG

#ifdef WIFI_DEBUG
#define LOG(fmt, ...) 	          printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif


#define WIFI_ATF_EN               (COMM_ON)
#define WIFI_CHECK_AP_CONN_STA_EN (COMM_OFF)

#define WIFI_PERIOD               (20U)     // RTOS period is 20ms
// Check AP connect status period 
#define WIFI_CONN_STA_CHK_PERIOS  (200000U)  
#define WIFI_CONN_PREIOD          ((uint32_t)((WIFI_CONN_STA_CHK_PERIOS) / (WIFI_PERIOD)))  // 10s

#define WIFI_RESET_PIN            (BSP_IO_PORT_06_PIN_08)


typedef struct
{
    int sec;
    int min;
    int hour;
    int mday;
    int mon;
    int year;
} s_Da16200TimeType;


#define WIFI_TCP_DATA_LEN            (128U)

static e_WifiOptType wifi_opt_mode = WIFI_OPT_INIT;
static e_WifiOptType wifi_opt_mode_pre = WIFI_OPT_INIT;
static e_ApConnectStatusType wifi_conn_status = WIFI_DISEONNECTED;
static e_WifiIotConnStaType wifi_iot_conn_status = WIFI_ONENET_DISCONNECTED; 
static uint16_t wifi_conn_sta_check_cnt = 0U;
static uint16_t wifi_tcp_send_cnt = 0U;


static char wifi_tcp_str[WIFI_TCP_DATA_LEN];
// TCP transmittion head '<ESC>S'
static char *wifi_tcp_head = "S1";
// IP address and port in Onenet TCP transparent transmission 
static char *wifi_tcp_ip_port = ",183.230.40.40,1811,";
// User information in Onenet TCP transparent transmission
static char *wifi_tcp_info = "*575047#ra4m2info#ra4m2*\r\n";


static fsp_err_t Wifi_Setting(void);
static fsp_err_t Wifi_ConnectToAp(void);
static fsp_err_t Wifi_ConnectToTcp(void);
static fsp_err_t Wifi_SntpStart(void);
static fsp_err_t Wifi_GetTime(void);
static void Wifi_OperateModeManage(void);
#if (WIFI_CHECK_AP_CONN_STA_EN == STD_ON)
static void Wifi_JudgeConnectStatusPeriod(void);
#endif 
static e_ApConnectStatusType Wifi_GetAPConnectStatus(void);


// Da16200 wifi initialization
void Wifi_Init(void)
{
    // reset wifi module first
    R_IOPORT_PinWrite(&g_ioport_ctrl, WIFI_RESET_PIN, BSP_IO_LEVEL_LOW);
    DELAY_MS(10);
    R_IOPORT_PinWrite(&g_ioport_ctrl, WIFI_RESET_PIN, BSP_IO_LEVEL_HIGH);

	// Initialize the timer for counter
    g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
    g_timer0.p_api->start(g_timer0.p_ctrl);
	// Initialize the uart for AT command communication 
    g_uart0.p_api->open(g_uart0.p_ctrl, g_uart0.p_cfg);

	wifi_opt_mode = WIFI_OPT_SETTING;
    LOG("Wifi: Wifi initialization success\n");
} 

// setting wifi module da16200
static fsp_err_t Wifi_Setting(void)
{
    fsp_err_t status = FSP_ERR_ASSERTION;
	uint8_t resp_buf[DA16200_STR_LEN_128];

	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));

	// ATZ command
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_ATZ, resp_buf);
    if(status != FSP_SUCCESS)
    {
		LOG("Wifi: Da16200 ATZ command failed!\n");
        return status;
    }
    DELAY_MS(10);

	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
	// ATE command
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_ATE, resp_buf);
    if(status != FSP_SUCCESS)
    {
		LOG("Wifi: Da16200 ATE command failed!\n");
        return status;
    }
    DELAY_MS(10);

#if (WIFI_ATF_EN == COMM_ON) // do not clear ap information
    /** Clear respond memory **/
    memset (resp_buf, 0 , sizeof(resp_buf));
    // ATF command
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_ATF, resp_buf);
    if(status != FSP_SUCCESS)
    {
        LOG("Wifi: Da16200 ATF command failed!\n");
        return status;
    }
    DELAY_MS(10);
#endif 

    /** Clear respond memory **/
    memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TMRFNOINIT, resp_buf);
    if(status != FSP_SUCCESS)
    {
        return status;
    }
    
    DELAY_MS(10);
	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_WFMODE, resp_buf);
    if(status != FSP_SUCCESS)
    {
        return status;
    }
    
    DELAY_MS(10);
	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_RESTART, resp_buf);
    if(status != FSP_SUCCESS)
    {
        return status;
    }

    DELAY_MS(10);

    return status;
}

// control wifi module connecting to AP
static fsp_err_t Wifi_ConnectToAp(void)
{
    fsp_err_t status = FSP_ERR_ASSERTION;
	uint8_t resp_buf[DA16200_STR_LEN_128];

#if (WIFI_ATF_EN == COMM_OFF)
    // update the AP connection status
    wifi_conn_status = Wifi_GetAPConnectStatus();
    DELAY_MS(10);
#endif
    // not connected
    if (WIFI_DISEONNECTED == wifi_conn_status)
    {
        /** Clear respond memory **/
        memset (resp_buf, 0 , sizeof(resp_buf));
        status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_WFJAP, resp_buf);
        if(status != FSP_SUCCESS)
        {
            return status;
        }
        /* Wifi connected to AP */
        wifi_conn_status = WIFI_CONNECTED;
    }
    DELAY_MS(10);

    // check DHC
    /** Clear respond memory **/
    // memset (resp_buf, 0 , sizeof(resp_buf));
    // status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_NWDHC, resp_buf);
    // if(status != FSP_SUCCESS)
    // {
    //     return status;
    // }
    // DELAY_MS(10);

    return status;
}

// setting sntp
static fsp_err_t Wifi_SntpStart(void)
{
    fsp_err_t status = FSP_ERR_ASSERTION;
	uint8_t resp_buf[DA16200_STR_LEN_32];

	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TZONE_GET, resp_buf);
    if(status != FSP_SUCCESS)
    {
       return status;
    }
    DELAY_MS(10);

    //+TZONE:28800: 8h for sync to bejing time
    if (SF_WIFI_TRUE != is_str_present((const char *)resp_buf, "+TZONE:28800"))
    {
        // sync to time zone to beijing time
        /** Clear respond memory **/
        memset (resp_buf, 0 , sizeof(resp_buf));
        status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TZONE_SET, resp_buf);
        if(status != FSP_SUCCESS)
        {
            return status;
        }
        DELAY_MS(10);
    }

	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_NWSNTP_STATUS, resp_buf);
    if(status != FSP_SUCCESS)
    {
       return status;
    }
    DELAY_MS(10);

    if (SF_WIFI_TRUE == is_str_present((const char *)resp_buf, (const char*)"+NWSNTP:1"))
    {
        // sntp is started, stop it now
        /** Clear respond memory **/
        memset (resp_buf, 0 , sizeof(resp_buf));
        status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_NWSNTP_STOP, resp_buf);
        if(status != FSP_SUCCESS)
        {
            return status;
        }
        DELAY_MS(10);
    }

    /** Clear respond memory **/
    memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_NWSNTP_START, resp_buf);
    if(status != FSP_SUCCESS)
    {
        return status;
    }
    DELAY_MS(10);

    return status;
}

// get da16200 time
static fsp_err_t Wifi_GetTime(void)
{
    fsp_err_t status = FSP_ERR_ASSERTION;
	uint8_t resp_buf[DA16200_STR_LEN_64];
    // time prase
    s_Da16200TimeType cur_t = {0,0,0,0,0,0};
    uint8_t i;
    uint8_t flag = 0;
    uint8_t j = 0;
    // get latest time
    rtc_time_t sntp_t;
    time_t sntp_t_stamp;

	/** Clear respond memory **/
	memset (resp_buf, 0 , sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TIME_GET, resp_buf);
    if(status != FSP_SUCCESS)
    {
       return status;
    }

    for (i = 0; i < DA16200_STR_LEN_64; i++)
    {
        if ((resp_buf[i] == '+') && (resp_buf[i+1] == 'T') && (resp_buf[i+2] == 'I') && 
            (resp_buf[i+3] == 'M'))
        {
            // find '+TIME:'
            flag = 1;
            i += 6;
        }
        else if (resp_buf[i] == '\n')
        {
            // stop
            flag = 0;
            if (flag != 0)
            {
                break;
            }
        }

        if (flag == 1)  // year
        {
            cur_t.year = cur_t.year  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 4)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
        else if (flag == 2)  // month
        {
            cur_t.mon = cur_t.mon  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 2)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
        else if (flag == 3)  // day
        {
            cur_t.mday = cur_t.mday  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 2)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
        else if (flag == 4)  //hour
        {
            cur_t.hour = cur_t.hour  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 2)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
        else if (flag == 5)  //minute
        {
            cur_t.min = cur_t.min  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 2)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
        else if (flag == 6)  //second
        {
            cur_t.sec = cur_t.sec  * 10 + (resp_buf[i] - '0');
            j++;
            if (j == 2)
            {
                j = 0;
                flag ++;
                i++;
            }
        }
    }

    LOG("Time is %d-%d-%d,%d:%d:%d\n", cur_t.year,
                                            cur_t.mon,
                                            cur_t.mday,
                                            cur_t.hour,
                                            cur_t.min,
                                            cur_t.sec);

    sntp_t.tm_year = cur_t.year - 1900;
    sntp_t.tm_mon = cur_t.mon - 1;
    sntp_t.tm_mday = cur_t.mday;
    sntp_t.tm_hour = cur_t.hour;
    sntp_t.tm_min = cur_t.min;
    sntp_t.tm_sec = cur_t.sec;

    // convert the sntp time to timestamp
    sntp_t_stamp = mktime(&sntp_t);
    LOG("SNTP timestamp is %d\n", sntp_t_stamp);

    // compare the sntp time and system rtc time
    rtc_time_t rtc_t;
    time_t rtc_t_stamp;

    rtc_t = Rtc_GetDateTime();

    // convert it to timestamp
    rtc_t_stamp = mktime(&rtc_t);
    LOG("RTC timestamp is %d\n", rtc_t_stamp);
    
    // if sntp time is a latest time, then update it to rtc
    if (rtc_t_stamp < sntp_t_stamp)
    {
        // update time to rtc
        Rtc_SetDateTime(&sntp_t);
    }
    else if (rtc_t_stamp > sntp_t_stamp)
    {
        // failed
        return FSP_ERR_ASSERTION;
    }

    return status;
}

// Judge the AP connection status
static e_ApConnectStatusType Wifi_GetAPConnectStatus(void)
{
    fsp_err_t status = FSP_SUCCESS;
    e_ApConnectStatusType ret_val = WIFI_DISEONNECTED;
    uint8_t resp_buf[DA16200_STR_LEN_32];

    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_WFSTA, resp_buf);
    if(status == FSP_SUCCESS)
    {
        if (SF_WIFI_TRUE == is_str_present((const char *)resp_buf, "+WFSTA:1"))
        {
            // Connect AP successfully
            ret_val = WIFI_CONNECTED;
        }
    }

    return ret_val;
}

// Connect TCP server
static fsp_err_t Wifi_ConnectToTcp(void)
{
    fsp_err_t status = FSP_ERR_ASSERTION;
	char resp_buf[DA16200_STR_LEN_128];


    /** Clear respond memory **/
    memset (resp_buf, 0, sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TRTRM, (uint8_t*)resp_buf);
    // Do not need response due to this command may be return ERROR:-99
    // if((status != FSP_SUCCESS))
    // {
    //     return status;
    // }

    /** Clear respond memory **/
    memset (resp_buf, 0, sizeof(resp_buf));
    status = Da1620_ATCommandExe(DA16200_AT_CMD_INDEX_AT_TRTC, (uint8_t*)resp_buf);
    if(status != FSP_SUCCESS)
    {
        return status;
    }

    // send user information
    if (pdTRUE == xSemaphoreTake(wifi_mutex, portMAX_DELAY))
    {
        /** Clear respond memory **/
        memset (resp_buf, '\0', sizeof(resp_buf));
        sprintf(resp_buf, "%s%d%s%s", wifi_tcp_head, 24, wifi_tcp_ip_port, wifi_tcp_info);
        /** AT MODODR command **/
        wifi_serial_write((uint8_t *)resp_buf, (uint16_t)strlen(resp_buf));
        LOG("==>%s\n", resp_buf);

        DELAY_MS(1000);
        LOG("Sent TCP user info(ONENET info)\n");

        wifi_iot_conn_status = WIFI_ONENET_CONNECTED;

        // release the mutex, unlock
        (void)xSemaphoreGive(wifi_mutex);
    }

    return status;
}

// update the Data to Server
/*
使用的是OneNet透传，数据格式是我自定义的。
数据格式为，onenet的Lua脚本会解析这个格式的数据并显示在云平台：
T+温度+H+湿度+L+亮度+M+可燃气体浓度ppm+P+台灯光亮度
举例：
T26.20H89.20L1234M2456P100：
表示温度为26.20摄氏度
湿度为89.20%
亮度为1234Lux
可燃气体浓度为2456ppm
台灯亮度为100%
##############################################################################
Lua脚本(需要实现device_data_analyze函数即可)：
function device_data_analyze(dev)
	local t={}
	local a=0

	-- 添加用户自定义代码 --
	-- 例如： --
	local s = dev:size()
    local temp = 0
    local str_end = 0
    local humi = 0
    local lux = 0
	local mq5_ppm = 0
	local lamp_pwm = 0

	-- 找到关键字 -- 
	-- T后面是温度，单位摄氏度 -- 
	-- H后面是湿度，单位百分比 -- 
	-- L后面是环境光亮度，单位Lux -- 
	-- M后面是环境可燃气体浓度，单位是ppm -- 
	-- P后面是台灯亮度，单位百分比 -- 
    temp, str_end = string.find(dev:bytes(1, s), "T")
    humi, str_end = string.find(dev:bytes(1, s), "H")
    lux, str_end = string.find(dev:bytes(1, s), "L")
	mq5_ppm, str_end = string.find(dev:bytes(1, s), "M")
	lamp_pwm, str_end = string.find(dev:bytes(1, s), "P")

    add_val(t, "temp", a, dev:bytes(temp+1, humi-temp-1))
    add_val(t, "humi", a, dev:bytes(humi+1, lux-humi-1))
    add_val(t, "Lux", a, dev:bytes(lux+1, mq5_ppm-lux-1))
	add_val(t, "combustible_gas", a, dev:bytes(mq5_ppm+1, lamp_pwm-mq5_ppm-1))
	add_val(t, "DeskLampBrightness", a, dev:bytes(lamp_pwm+1, s-lamp_pwm))

	-- 成功收到数据后的响应 -- 
    dev:response()
	dev:send("received")
	-- return $1,$2 --
	-- 例如： --
	return s,to_json(t)
end
*/
static void Wifi_UpdateSensorData(void)
{
    uint32_t lux = Isl29035_ReadData();
    s_Hs300xDataType humi_temp = Hs300x_ReadData();
    uint32_t mq5_ppm = Mq5_GetGasPpmValue();
    uint32_t lamp_pwm = Led_LampPwmGet();
    uint8_t data_len;
    uint16_t bytes_read = 0U;
    uint8_t resp_buff[DA16200_STR_LEN_64] = {0};
    char temp[64];
    fsp_err_t result;

    /** Clear respond memory **/
    memset (wifi_tcp_str, '\0', WIFI_TCP_DATA_LEN);
    // send the data to onenet
    sprintf(temp, "T%.2fH%.2fL%dM%dP%d", humi_temp.temp, humi_temp.humi, lux, mq5_ppm, lamp_pwm);
    data_len = (uint8_t)strlen((const char *)temp);
    sprintf(wifi_tcp_str, "%s%d%s%s\r\n", wifi_tcp_head, data_len, wifi_tcp_ip_port, temp);
    LOG ("Send data to TCP server: %s\n", wifi_tcp_str);

    /** Clear respond memory **/
    memset (resp_buff, '\0', bytes_read);

    // setting mutex for lock wifi uart
    if (pdTRUE == xSemaphoreTake(wifi_mutex, portMAX_DELAY)) 
    {
        /** AT MODODR command **/
        wifi_serial_write((uint8_t *)wifi_tcp_str, (uint16_t)strlen(wifi_tcp_str));

        bytes_read = DA16200_STR_LEN_64;
        result = wifi_serial_read(resp_buff, &bytes_read, (const char *)"received", 1000);
        if (FSP_SUCCESS != result)
        {
            // Go to check the AP connect status when failed
            wifi_opt_mode_pre = wifi_opt_mode;
            wifi_opt_mode = WIFI_OPT_CONNECT_STATUS;
        }
        else
        {
            
        }
        // release the mutex, unlock
        (void)xSemaphoreGive(wifi_mutex);
    }
}   

// Operation mode of wifi running
static void Wifi_OperateModeManage(void)
{
    fsp_err_t err;

    switch(wifi_opt_mode)
    {
        case WIFI_OPT_SETTING:
        {
            err = Wifi_Setting();
            if(err)
            {
                LOG("Wifi: Fail to setting\nRetry Start...\n");
                DELAY_MS(2000);
            }
            else
            {
                LOG("Wifi: Sucess to setting\n");
                wifi_opt_mode_pre = wifi_opt_mode;
                wifi_opt_mode = WIFI_OPT_CONNECT;
            }
            
            break;
        }
        case WIFI_OPT_CONNECT:
        {
            err = Wifi_ConnectToAp();
            if(err)
            {
                LOG("Wifi: Fail to connected to AP\nRetry Start...\n");
                DELAY_MS(500);
            }
            else
            {
                LOG("Wifi: Sucess to connected to AP\n");
                wifi_opt_mode_pre = wifi_opt_mode;
                wifi_opt_mode = WIFI_OPT_SNTP_SETTING;
            }
            break;
        }
        case WIFI_OPT_SNTP_SETTING:
        {
            err = Wifi_SntpStart();
            if(err)
            {
                LOG("Wifi: Fail to start SNTP\nRetry Start...\n");
                DELAY_MS(500);
            }
            else
            {
                LOG("Wifi: Sucess to start SNTP\n");
                wifi_opt_mode_pre = wifi_opt_mode;
                wifi_opt_mode = WIFI_OPT_GET_TIME;
            }
            break;
        }
        case WIFI_OPT_GET_TIME:
        {
            // update the time
            err = Wifi_GetTime();
            if(err)
            {
                LOG("Wifi: Fail to get time from da16200\nRetry Start...\n");
                DELAY_MS(1000);
            }
            else
            {
                LOG("Wifi: Sucess to get time from da16200\n");
                wifi_opt_mode_pre = wifi_opt_mode;
                wifi_opt_mode = WIFI_OPT_CONNECT_TCP;
            }
            break;
        }
        case WIFI_OPT_CONNECT_TCP:
        {
            // Connect onenet Iot
            err = Wifi_ConnectToTcp();
            if(err)
            {
                LOG("Wifi: Fail to connect to the TCP server\nRetry Start...\n");
                DELAY_MS(500);
            }
            else
            {
                LOG("Wifi: Sucess to connect to the TCP server\n");
                wifi_opt_mode_pre = wifi_opt_mode;
                wifi_opt_mode = WIFI_OPT_WORKING;
                wifi_tcp_send_cnt = 0U;
            }
            break;
        }
        case WIFI_OPT_WORKING:
        {
            // Used for connect with ONENET TCP server
            if (wifi_tcp_send_cnt >= 30U)
            {
                wifi_tcp_send_cnt = 0U;
                // send the sensor data every 5s
                Wifi_UpdateSensorData(); 
            }
            else
            {
                wifi_tcp_send_cnt ++;
            }
            
            Da16200_FeedbackHandle();
            DELAY_MS(100);
            break;
        }
        case WIFI_OPT_CONNECT_STATUS:
        {
            // update the AP connection status
            wifi_conn_status = Wifi_GetAPConnectStatus();

            // re-try to connecting the AP
            if (WIFI_DISEONNECTED == wifi_conn_status)
            {
                // re-try to connecting the AP       
                wifi_opt_mode = WIFI_OPT_CONNECT;
                wifi_opt_mode_pre = wifi_opt_mode;
            }
            else
            {
                // switch to previous operate mode
                wifi_opt_mode = wifi_opt_mode_pre;
                wifi_opt_mode_pre = WIFI_OPT_CONNECT_STATUS;
            }

            DELAY_MS(1);
            break;
        }
        default:
        {
            LOG("Wifi: Error operation");
			DELAY_MS(1000);
            break;
        }
    }
}

#if (WIFI_CHECK_AP_CONN_STA_EN == STD_ON)
// check the connect status
static void Wifi_JudgeConnectStatusPeriod(void)
{
    // not in wifi initialization mode
    if ((wifi_opt_mode != WIFI_OPT_INIT) && 
        (wifi_opt_mode != WIFI_OPT_SETTING) && 
        (wifi_opt_mode != WIFI_OPT_CONNECT)// &&
        // (wifi_opt_mode != WIFI_OPT_WORKING)
        )
    {
        if (wifi_conn_sta_check_cnt >= WIFI_CONN_PREIOD)
        {
            wifi_conn_sta_check_cnt = 0U;

            LOG("Wifi: Check cnnect status");

            wifi_opt_mode_pre = wifi_opt_mode;
            wifi_opt_mode = WIFI_OPT_CONNECT_STATUS;      
        }
    }
}
#endif

// Wifi working main interface for task
void Wifi_RunningEntry(void)
{
    Wifi_OperateModeManage();
#if (WIFI_CHECK_AP_CONN_STA_EN == STD_ON)
    Wifi_JudgeConnectStatusPeriod();
#endif 
    vTaskDelay(pdMS_TO_TICKS(WIFI_PERIOD));
}

e_ApConnectStatusType Wifi_GetApConnStatus(void)
{
    return wifi_conn_status;
}


e_WifiIotConnStaType Wifi_GetIotConnStatus(void)
{
    return wifi_iot_conn_status;
}

void WIfi_CheckPreiodLoop(void)
{
    if (wifi_conn_sta_check_cnt < WIFI_CONN_PREIOD)
    {
        wifi_conn_sta_check_cnt ++;
    }
}

/*
使用的是OneNet的TCP透传，数据格式是我自定义的。
控制台灯的数据命令是：Lamp:+百分比台灯亮度
比如：
Lamp:100  - 表示台灯打开，亮度为100%
Lamp:0    - 表示关闭台灯
Lamp:80   - 表示台灯亮度设置为80%
*/
void Da16200_TRDTC_response(char *resp_data, uint16_t resp_data_len)
{
    char *sub_str_ptr;
    int command = -1;
	
	(void)resp_data_len;

    // find the sub-string 'Lamp'
    sub_str_ptr = strstr((const char *)resp_data, "Lamp");
    if (NULL != sub_str_ptr)
    {
        // parse the control command
        sscanf(sub_str_ptr, "Lamp:%d", &command);
        if ((command >= 0) && (command <= 100))
        {
            // the command is valid
            Led_LampPwmCtrl((uint8_t)command);
            LOG ("Wifi: Lamp control command is %d\n", command);
        }
        else
        {
            LOG ("Wifi: Invalid Lamp control command\n");
        }
    }
}
